package com.yyfax.pay.third.service;

import java.io.Serializable;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.yyfax.commons.json.JacksonUtil;
import com.yyfax.framework.yyfax.enums.RspCodeEnum;
import com.yyfax.pay.common.enums.PayModeEnum;
import com.yyfax.pay.common.enums.PayTypeEnum;
import com.yyfax.pay.common.enums.PaymentEnum;
import com.yyfax.pay.common.exception.YYPayException;
import com.yyfax.pay.config.data.service.MerchantPaymentService;
import com.yyfax.pay.config.model.po.MerchantPaymentPO;

/**
 * 支付常量信息服务
 * 
 * @author heizq
 * @date 2017-05-02 09:11:09
 * @since v1.0.0
 */
@Service
public class PayInfoConstantService {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private MerchantPaymentService merchantPaymentService;

	/**
	 * 根据商户号获取商户支付信息
	 * 
	 * @author heizq
	 * @date 2017-05-02 09:09:46
	 * @since v1.0.0
	 * @param merchantCode 商户号
	 * @param payment 支付通过
	 * @param payType 支付类型
	 * @param payMode 支付模式
	 * @return
	 * @throws Exception
	 */
	public <T extends Serializable> T getMerchantInfo(Class<T> clazz, String merchantCode, PaymentEnum payment, PayTypeEnum payType,
			PayModeEnum payMode) throws Exception {
		MerchantPaymentPO merchant = merchantPaymentService.queryByThirdMerchantCode(merchantCode, payment.getValue(), payType.getValue(),
				payMode.getValue());
		T constantExt = null;
		if (merchant == null) {
			logger.error("【pay】查询支付通道信息失败,商户信息为空,merchantCode:{}", merchantCode);
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_OBJECT_NOT_FOUND.getValue(), "查询支付通过信息失败,商户信息为空.");
		}
		if (StringUtils.isNotBlank(merchant.getExt())) {
			constantExt = JacksonUtil.nonullMapper.fromJson(merchant.getExt(), clazz);
		} else {
			logger.error("【pay】支付商户扩展信息为空,merchantCode:{}", merchantCode);
			throw new YYPayException(RspCodeEnum.COMMON_ERROR_OBJECT_NOT_FOUND.getValue(), "支付通过扩展信息为空");
		}
		logger.debug("【pay】查询支付通道信息成功,merchant:{}", JSON.toJSONString(merchant));
		return constantExt;
	}

}
